﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Templete
{
    public partial class Form1 : Form
    {
        int imageCount=0;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Image img = ScreenCapture(pictureBox1.Width, pictureBox1.Height, pictureBox1.Location);
            pictureBox1.Image = img;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Image img = ScreenCapture(pictureBox1.Width, pictureBox1.Height, pictureBox1.Location);
            pictureBox1.Image = img;
            ExportToBmp(pictureBox1, @"temp" + (++imageCount).ToString() + ".png");
            pictureBox1.Image = null;
        }

        private Image ScreenCapture(int nBitmapWidth, int nBitmapHeight, Point ptSource)
        {
            Size size = new Size(nBitmapWidth, nBitmapHeight);
            Bitmap bmp = new Bitmap(nBitmapWidth, nBitmapHeight);
            Graphics g = Graphics.FromImage(bmp);
            g.CopyFromScreen(ptSource, new Point(0,0), new Size(nBitmapWidth, nBitmapHeight));
            return bmp as Image;
        }

        public void ExportToBmp(PictureBox pictureBox,string path)
        {
           using (var bitmap = new Bitmap(pictureBox1.Width,pictureBox.Height))
           {
               pictureBox.DrawToBitmap(bitmap, pictureBox.ClientRectangle);
               
               System.Drawing.Imaging.ImageFormat imageFormat = null;
 
               var extension = System.IO.Path.GetExtension(@path);
               switch (extension.ToLower())
               {
                    case".bmp":
                        imageFormat =System.Drawing.Imaging.ImageFormat.Bmp;
                        break;
                    case".png":
                        imageFormat =System.Drawing.Imaging.ImageFormat.Png;
                        break;
                    case".jpeg":
                    case".jpg":
                        imageFormat =System.Drawing.Imaging.ImageFormat.Jpeg;
                        break;
                    case".gif":
                        imageFormat =System.Drawing.Imaging.ImageFormat.Gif;
                        break;
                    default:
                        throw new NotSupportedException("File extension is not supported");
               }
 
               bitmap.Save(path, imageFormat);
           }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Image img = ScreenCapture(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, new Point(0,0));
            pictureBox1.Image = img;
            ExportToBmp(pictureBox1, @"temp" + (++imageCount).ToString() + ".png");
            pictureBox1.Image = null;
        }
    }
}
